package cn.fczds.camhelper.rider.service.impl;

import cn.fczds.camhelper.order.mapper.OrderMapper;
import cn.fczds.camhelper.order.pojo.vo.OrderVO;
import cn.fczds.camhelper.rider.mapper.RiderMapper;
import cn.fczds.camhelper.rider.pojo.vo.RecentOrdersVO;
import cn.fczds.camhelper.rider.pojo.vo.RiderDateVO;
import cn.fczds.camhelper.rider.pojo.vo.RiderVO;
import cn.fczds.camhelper.rider.service.RiderService;
import cn.fczds.camhelper.user.mapper.UserMapper;
import cn.fczds.camhelper.user.pojo.entity.User;
import cn.fczds.camhelper.user.pojo.vo.UserVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.Month;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class RiderServiceImpl implements RiderService {
    @Autowired
    RiderMapper riderMapper;
    @Autowired
    UserMapper userMapper;
    @Autowired
    OrderMapper orderMapper;

    @Override
    public RiderVO select(Long userId) {
        RiderVO riderVO = new RiderVO();
        List<OrderVO> list = riderMapper.selectOrderListBy3(userId);
        log.debug("list={}",list);
        log.debug("userId={}",userId);
        UserVO userVo = userMapper.selectById(userId);
        log.debug("userVo={}", userVo);
        riderVO.setRiderLev(userVo.getRiderLevel());
        log.debug("set level : riderVO = {}", riderVO);
        riderVO.setOrderNum(riderMapper.selectOrderCount(userId));
        log.debug("set OrderNum : riderVO = {}", riderVO);
        int[] num = new int[6];
        double a = 0;
        int len = list.size();
        LocalDate localDate = LocalDate.now();
        localDate.getDayOfWeek();
        log.debug("localDate={}", localDate);
        Month month = localDate.getMonth();
        log.debug("month={}", month);
        BigDecimal monthCount  = BigDecimal.valueOf(0);
        BigDecimal dayCount  = BigDecimal.valueOf(0);
        for(int i = 0;i<list.size();i++)
        {
            OrderVO orderVO = list.get(i);
            log.debug("orderVO = {}", orderVO);
            if(!(orderVO.getEndTime()==null || orderVO.getEndTime().equals(""))){
                LocalDate localDate1 = orderVO.getEndTime().toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
                log.debug("localDate1 = {}", localDate1);
                Month month1 = localDate1.getMonth();
                log.debug("month1 = {}", month1);
                if(localDate.equals(localDate1))
                    dayCount = dayCount.add(orderVO.getOrderPrice());
                if(month.equals(month1))
                    monthCount = monthCount.add(orderVO.getOrderPrice());

            }
            if(orderVO.getOrderRate()==null || orderVO.getOrderRate().equals("")){
            len--;
             }else{
                a += Double.parseDouble(orderVO.getOrderRate());
            }
            System.out.println(a);
            if(orderVO.getOrderType().equals("拿外卖")){
                num[0]++;
            }else if(orderVO.getOrderType().equals("打饭")){
                num[1]++;
            }else if(orderVO.getOrderType().equals("取物品")){
                num[2]++;
            }else if(orderVO.getOrderType().equals("拿快递")){
                num[3]++;
            }else if(orderVO.getOrderType().equals("跑腿")){
                num[4]++;
            }else{
                num[5]++;
            }
        }
        riderVO.setTodayIncome(dayCount.toString());
        riderVO.setMonthIncome(monthCount.toString());
        int max = 0;
        int maxNum = 0;
        for(int i = 0;i<num.length;i++){
            if(num[i]>max){
                max = num[i];
                maxNum = i;
            }
        }
        String type = "";
        if(maxNum == 0){
            type = "拿外卖";
        }else if(maxNum == 1){
            type = "打饭";
        }else if(maxNum == 2){
            type = "取物品";
        }else if(maxNum == 3){
            type = "拿快递";
        }else if(maxNum == 4){
            type = "跑腿";
        }else {
            type = "校园送";
        }
        riderVO.setPreference(type);
        log.debug("set Preference : riderVO = {}", riderVO);
        double one = a/len;
        String str = String.format("%.1f",one);
        riderVO.setAvgRate(str);
        log.debug("set AvgRate : riderVO = {}", riderVO);
        List<RecentOrdersVO> vos = new ArrayList<>();
        int q = 3;
        if(list.size()<3)
            q = list.size();
        for(int i = 0;i<q;i++)
        {
            RecentOrdersVO vos1 = new RecentOrdersVO();
            OrderVO orderVO = list.get(i);
            log.debug("orderVO = {}", orderVO);
            log.debug("vos = {}",vos);
            UserVO userVO = userMapper.selectById(orderVO.getUserId());
            log.debug("userVO = {}", userVO);
            if(!(orderVO.getOrderTitle()==null || orderVO.getOrderTitle().equals(""))){
                vos1.setTitle(orderVO.getOrderTitle());
            }
            log.debug("set OrderTitle : vos[i] = {}", vos1);
            if(!(orderVO.getEndTime()==null || orderVO.getEndTime().equals(""))){
                vos1.setEndTime(orderVO.getEndTime());
            }
            log.debug("set EndTime : vos[i] = {}",vos1);
            if(!(userVO.getAvatarImgUrl()==null || userVO.getAvatarImgUrl().equals(""))) {
                vos1.setAvatarUrl(userVO.getAvatarImgUrl());
            }
            log.debug("set AvatarUrl : vos[i] = {}", vos1);
            if(!(orderVO.getOrderReview()==null || orderVO.getOrderReview().equals(""))){
                vos1.setOrderReview(orderVO.getOrderReview());
            }
            log.debug("set OrderReview : vos[i] = {}", vos1);
            vos1.setUsername(userVO.getUsername());
            log.debug("set Username : vos[i] = {}", vos1);
            vos1.setOrderRate(orderVO.getOrderRate());
            log.debug("set OrderRate : vos[i] = {}", vos1);
            String[] teps = new String[2];
            teps[0] = orderVO.getEndLocation();
            teps[1] = orderVO.getOrderType();
            vos1.setTags(teps);
            log.debug("set Tags : vos[i] = {}", vos1);
            vos.add(vos1);
        }
        riderVO.setRecentOrders(vos);
        log.debug("set RecentOrders : riderVO = {}", riderVO);


        return riderVO;
    }

    @Override
    public RiderDateVO selectDate(Long userId) {
        RiderDateVO riderDateVO = new RiderDateVO();
        List<OrderVO> list = riderMapper.selectOrderListBy3(userId);
        log.debug("list={}",list);
        log.debug("userId={}",userId);
        UserVO userVo = userMapper.selectById(userId);
        log.debug("userVo={}", userVo);
        riderDateVO.setRiderLev(userVo.getRiderLevel().toString());
        riderDateVO.setOrderNum((long)list.size());
        int len = list.size();
        double a = 0;
        long q = 0;
        long good = 0;
        long bad = 0;
        LocalDate localDate = LocalDate.now();
        List<LocalDate> localDateList = new ArrayList<>();
        List<String> dateWeek = new ArrayList<>();
        List<Long> dateCount = new ArrayList<>();
        for(int i = 0;i<7;i++)
        {
            localDate = localDate.minusDays(1);
            localDateList.add(localDate);
            dateWeek.add(localDate.toString());
            dateCount.add(0L);
        }
        for(int i = 0;i<list.size();i++){
            OrderVO orderVO = list.get(i);
            System.out.println(orderVO);
            if((!(orderVO.getEndTime()==null || orderVO.getEndTime().equals(""))))
            {
                LocalDate localDate1 = orderVO.getEndTime().toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
                for(int j =0;j<7;j++)
                {
                    if(localDateList.get(j).equals(localDate1)){
                        dateCount.set(j,dateCount.get(j)+1);
                    }
                }
            }
            if(!(orderVO.getOrderRate()==null || orderVO.getOrderRate().equals(""))){
                double w = Double.parseDouble(orderVO.getOrderRate());
                a += w;
                if(w<2)
                    bad++;
                else if(w>=4) good++;
                System.out.println(a);
            }else{
                len--;
            }

            if(orderVO.getOrderStatus().equals("已完成"))
                q++;
        }

        riderDateVO.setGood(good);
        log.debug("good:",riderDateVO);
        riderDateVO.setBad(bad);
        log.debug("bad:",riderDateVO);
        double one = a/len;
        String str = String.format("%.1f",one);
        riderDateVO.setAvgRate(str);
        log.debug("avgRate:",str);
        riderDateVO.setFinishNum(q);
        log.debug("finishNum:",riderDateVO);
        List<OrderVO> vos = new ArrayList<>();
        int t = 10;
        if(list.size()<10)
            t = list.size();
        for(int i = 0;i<t;i++)
        {
            vos.add(list.get(i));
        }
        riderDateVO.setOrderList(vos);
        log.debug("orderList:",riderDateVO);
        riderDateVO.setDate(dateWeek);
        log.debug("dateWeek:",riderDateVO);
        riderDateVO.setOrderNumDate(dateCount);
        log.debug("dateCount:",riderDateVO);
        riderDateVO.setAvgTime("30");
        riderDateVO.setOverTime(0L);
        return riderDateVO;
    }

    @Override
    public UserVO becomeRider(Long userId) {
        User user = new User();
        user.setUserId(userId);
        user.setLevel(20L);
        userMapper.update(user);
        return userMapper.selectById(userId);
    }
}
